// $ANTLR 3.3 Nov 30, 2010 12:50:56 /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g 2012-03-22 10:02:21

import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;

public class PLParser extends Parser {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "OP", "NUM", "WHITESPACE", "'('", "')'"
    };
    public static final int EOF=-1;
    public static final int T__7=7;
    public static final int T__8=8;
    public static final int OP=4;
    public static final int NUM=5;
    public static final int WHITESPACE=6;

    // delegates
    // delegators


        public PLParser(TokenStream input) {
            this(input, new RecognizerSharedState());
        }
        public PLParser(TokenStream input, RecognizerSharedState state) {
            super(input, state);
             
        }
        

    public String[] getTokenNames() { return PLParser.tokenNames; }
    public String getGrammarFileName() { return "/home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g"; }


      int nodeCount = 0;
      String newNode(String label) {
        String nodeId = "n" + nodeCount;
        nodeCount ++;
        System.out.println(nodeId + " [label=\"" + label + "\"];");
        return nodeId;
      }
      void connect(String n1, String n2) {
        System.out.println(n1 + " -> " + n2 + ";");
      }



    // $ANTLR start "start"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:16:1: start : expr EOF ;
    public final void start() throws RecognitionException {

          System.out.println("digraph G {");

        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:20:3: ( expr EOF )
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:20:5: expr EOF
            {
            pushFollow(FOLLOW_expr_in_start24);
            expr();

            state._fsp--;

            match(input,EOF,FOLLOW_EOF_in_start26); 
            System.out.println("}");

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "start"


    // $ANTLR start "expr"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:23:1: expr returns [String nodeId] : ( '(' OP (e= expr )* ')' | NUM );
    public final String expr() throws RecognitionException {
        String nodeId = null;

        Token OP1=null;
        Token NUM2=null;
        String e = null;


        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:24:3: ( '(' OP (e= expr )* ')' | NUM )
            int alt2=2;
            int LA2_0 = input.LA(1);

            if ( (LA2_0==7) ) {
                alt2=1;
            }
            else if ( (LA2_0==NUM) ) {
                alt2=2;
            }
            else {
                NoViableAltException nvae =
                    new NoViableAltException("", 2, 0, input);

                throw nvae;
            }
            switch (alt2) {
                case 1 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:24:5: '(' OP (e= expr )* ')'
                    {
                    match(input,7,FOLLOW_7_in_expr47); 
                    nodeId = this.newNode("expr");
                    OP1=(Token)match(input,OP,FOLLOW_OP_in_expr55); 

                              String opId = this.newNode((OP1!=null?OP1.getText():null));
                              this.connect(nodeId, opId);
                           
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:29:5: (e= expr )*
                    loop1:
                    do {
                        int alt1=2;
                        int LA1_0 = input.LA(1);

                        if ( (LA1_0==NUM||LA1_0==7) ) {
                            alt1=1;
                        }


                        switch (alt1) {
                    	case 1 :
                    	    // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:29:6: e= expr
                    	    {
                    	    pushFollow(FOLLOW_expr_in_expr66);
                    	    e=expr();

                    	    state._fsp--;


                    	          this.connect(nodeId, e);
                    	        

                    	    }
                    	    break;

                    	default :
                    	    break loop1;
                        }
                    } while (true);

                    match(input,8,FOLLOW_8_in_expr76); 

                    }
                    break;
                case 2 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/05-display-parsetree/src/PL.g:33:5: NUM
                    {
                    NUM2=(Token)match(input,NUM,FOLLOW_NUM_in_expr82); 

                        nodeId = this.newNode((NUM2!=null?NUM2.getText():null));
                      

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return nodeId;
    }
    // $ANTLR end "expr"

    // Delegated rules


 

    public static final BitSet FOLLOW_expr_in_start24 = new BitSet(new long[]{0x0000000000000000L});
    public static final BitSet FOLLOW_EOF_in_start26 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_7_in_expr47 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_OP_in_expr55 = new BitSet(new long[]{0x00000000000001A0L});
    public static final BitSet FOLLOW_expr_in_expr66 = new BitSet(new long[]{0x00000000000001A0L});
    public static final BitSet FOLLOW_8_in_expr76 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_NUM_in_expr82 = new BitSet(new long[]{0x0000000000000002L});

}